/* *****************************************************************************************
 
Made by www.Flashphoner.com, all rights reserved.
These sources can only be used for working with Flashphoner server.
If you want use this to create your own application (separate from Flashphoner server) � 
please contact support@flashphoner.com 

******************************************************************************************/

package com.flashphoner.api
{
	import com.adobe.cairngorm.model.IModelLocator;
	import com.flashphoner.Logger;
	import com.flashphoner.api.data.ErrorCodes;
	import com.flashphoner.api.data.PhoneConfig;
	import com.flashphoner.api.management.SoundControl;
	
	import flash.display.DisplayObjectContainer;
	import flash.events.*;
	import flash.media.*;
	import flash.net.*;
	import flash.utils.Timer;
	
	import mx.core.Application;
	
	/**
	 * Model object
	 * **/	
	[Bindable]
	internal class PhoneModel implements IModelLocator 
	{	
		private static var phoneModel:PhoneModel;
		
		public var initialized:Boolean = false;
		
		private var xml:XML;
		
		public var app : DisplayObjectContainer;
		private var phoneController:PhoneController;
		public var parameters:Object = null; 
				
		private static var initTimer:Timer;
				
		public static function getInstance():PhoneModel {
			if (phoneModel == null){
				phoneModel = new PhoneModel();
			}	
			return phoneModel;				
		}
				
		
		
		public function PhoneModel()
		{	
			super();
			init();
			startInitTimer();
			phoneController = new PhoneController();	
						
		}	
		
		private function init(event:TimerEvent = null):void{
			if (Application.application == null || Application.application.parameters == null){
				return;
			}else{
				dropInitTimer();
			}
			var loader:URLLoader = new URLLoader();
			var qwe:Object = Application.application.parameters;
			var config:String = "flashphoner.xml";
        	try{
				if (Application.application.parameters != null && Application.application.parameters.config!=null){
					config = Application.application.parameters.config;
				}
        	} catch (e:Error){
        		config = "flashphoner.xml";
        	}       			

			var request:URLRequest = new URLRequest(config);
			loader.load(request);
			loader.addEventListener(Event.COMPLETE, onComplete);
			loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR,seurityErrorHandler);
			loader.addEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler);
			
		}
		
		private function seurityErrorHandler(event:SecurityErrorEvent):void{
			throw new Error(event.toString());
		} 
		
		private function ioErrorHandler(event:IOErrorEvent):void{
			throw new Error(event.toString());
		}
		
		private function onComplete(event:Event):void
			{	
			    var loader:URLLoader = event.target as URLLoader;
			    if (loader != null)
			    {
			        xml = new XML(loader.data);
					var count:Number = xml.children().length();
					parameters = new Object();
					for (var i:Number = 0; i < count; i++)
					{
						var node:String =  xml.children()[i].localName().toString();
						parameters[node] = String(xml[node]); 
					}
			        
			        PhoneConfig.SERVER_URL = xml.rtmp_server;
			        PhoneConfig.APP_NAME = xml.application;
					var check_validation_callee:String = xml.check_validation_callee;
					PhoneConfig.CHECK_VALIDATION_CALLEE = (check_validation_callee == "true");
			        var regRequired:String = xml.register_required; 
			        PhoneConfig.REGISTER_REQUIRED = (regRequired == "true");
			        if (xml.ring_sound != null && xml.ring_sound != ""){
			        	SoundControl.RING_SOUND = xml.ring_sound;
			        }
			        if (xml.busy_sound != null && xml.busy_sound != ""){
			        	SoundControl.BUSY_SOUND = xml.busy_sound;
			        }
			        if (xml.register_sound != null && xml.register_sound != ""){
				        SoundControl.REGISTER_SOUND = xml.register_sound;
			        }
			        if (xml.finish_sound != null && xml.finish_sound != ""){
				        SoundControl.FINISH_SOUND = xml.finish_sound;
			        }
			        SoundControl.getInstance().updateSounds();
			    }
			    else
			    {
			        Logger.info("Can not load xml settings. Default settings will be used.");
			    }
			    initialized = true;
			} 

		
		public function dropInitTimer():void{
			if (initTimer!=null){
				initTimer.stop();
				initTimer.removeEventListener(TimerEvent.TIMER,init);
				initTimer=null;
			}
		}
		
		public function startInitTimer():void{
			if (initTimer!=null){
				initTimer.stop();
					
			} else {
				initTimer = new Timer(1000);
				initTimer.addEventListener(TimerEvent.TIMER,init);					
			}
			initTimer.start();	
		}		
		
	
	}
	
	
}